<?php

namespace app\admin\model\express;

use think\Model;
use app\admin\model\express\OrderGoods;

class Order extends Model
{

    // 表名
    protected $name = 'express_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'state_text',
        'status_text',
        'is_open_text',
        'is_delivery_text',
        'server_text',
        'goods_count'
    ];
    
    
    protected static function init()
    {
        self::afterDelete(function ($row) {
            $goods = new OrderGoods();
            OrderGoods::destroy(['order_id' => $row->id]);
        });
    }
    
    
    public function getGoodsCountAttr($value,$data){
        return count($this->Goods);
    }

    public function getServerTextAttr($value, $data)
    {
//        $value = $value ? $value : (isset($data['is_open']) ? $data['is_open'] : '');
//        $list = $this->getIsOpenList();
//        return isset($list[$value]) ? $list[$value] : '';

        return \app\admin\model\express\Service::where('id','in',$this->service_ids)->select();;
    }
    
    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1'), '2' => __('State 2'), '3' => __('State 3'), '4' => __('State 4')];
    }
    public function getStatusList()
    {
        return [
            'created' => __('Status created'),
            'complete' => __('Status complete'),
            'waitingpay' => __('Status waitingpay'),
            'successpay' => __('Status successpay'),
            'delivered' => __('Status delivered'),
        ];
    }


    public function getIsOpenList()
    {
        return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
    }

    public function getIsDeliveryList()
    {
        return ['0' => __('Is_delivery 0'), '1' => __('Is_delivery 1')];
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function getIsOpenTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_open']) ? $data['is_open'] : '');
        $list = $this->getIsOpenList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsDeliveryTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_delivery']) ? $data['is_delivery'] : '');
        $list = $this->getIsDeliveryList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goods(){
        return $this->hasMany('app\admin\model\express\OrderGoods','order_id','id');
    }


    public function setOrderNoAttr($value,$data){
        return 'TY'.substr(str_shuffle(substr(time(),-8).substr(substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8).$value,-8)),0,6);
    }
}
